package com.arouter.demo.searchmodule.viewmodel;

import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.callback.NavCallback;
import com.alibaba.android.arouter.launcher.ARouter;
import com.arouter.demo.baselib.router.RouterPath;
import com.arouter.demo.searchmodule.R;
import com.arouter.demo.searchmodule.entity.ObjEntity;
import com.arouter.demo.searchmodule.entity.ObjParcelable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * desc:
 * 2018-05-30 14:55
 */
public class SearchVm {

    private static FragmentActivity mActivity;

    public void onClick(final View view) {

        mActivity = (FragmentActivity) view.getContext();
        int viewId = view.getId();

        if (viewId == R.id.btn_jump_url) {
            ARouter.getInstance()
                   .build(RouterPath.WEB_ACTIVITY)
                   .withString("url", "file:///android_asset/schame-test.html")
                   .navigation();
            return;
        }

        if (viewId == R.id.btn_interceptor) {
            ARouter.getInstance()
                   .build(RouterPath.INTERCEPTOR_TEST)
                   .navigation(view.getContext(), new NavCallback() {
                       @Override
                       public void onArrival(Postcard postcard) {

                       }

                       @Override
                       public void onInterrupt(Postcard postcard) {
                           super.onInterrupt(postcard);
                           // 被拦截的逻辑处理
                           // Toast.makeText(view.getContext(), "被拦截了", Toast.LENGTH_SHORT).show();
                           Log.e("ARouter", "被拦截了");
                       }
                   });

            return;
        }
        if (viewId == R.id.btn_inject) {
            // 依赖注入

            ObjEntity objEntity = new ObjEntity();
            objEntity.setId(666);
            objEntity.setName("Jack");

            ObjParcelable objParcelable = new ObjParcelable(777, "Rose");


            List<ObjEntity> objList = new ArrayList<>();
            objList.add(objEntity);

            Map<String, List<ObjEntity>> map = new HashMap<>();
            map.put("testMap", objList);


            ARouter.getInstance().build(RouterPath.AUTO_INJECT)
                   .withObject("objEntity", objEntity)
                   .withParcelable("objParcelable", objParcelable)
                   .withObject("objList", objList)
                   .withObject("map", map)
                   .navigation();


        }


    }

    public static FragmentActivity getActivity() {
        return mActivity;
    }
}
